#include "star_net.h"
#include "ui_star_net.h"

Star_net::Star_net(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Star_net)
{
    ui->setupUi(this);
    st.loadstar1();
    inittable();

}

Star_net::~Star_net()
{
    delete ui;
}

void Star_net::inittable(){
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);       // 整行选中的方式
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);    // 设置为可以选中单个目标
    ui->tableWidget->verticalHeader()->setVisible(false);                       // 隐藏行表头
    ui->tableWidget->resizeRowsToContents();                                    // 行高与内容匹配
    ui->tableWidget->resizeColumnsToContents();
    ui->tableWidget->setRowCount(100);
    ui->tableWidget->setColumnCount(2);
    QHeaderView* widthlist = ui->tableWidget->horizontalHeader();
    widthlist->resizeSection(0, 100);									                                  // 设置列宽
    widthlist->resizeSection(1, 400);
    int i=0;
    while(st.starlist[i]->text!="<空>")
    {
        ui->tableWidget->setRowHeight(i , 64);//设置行的高度

        QTableWidgetItem *item_index = new QTableWidgetItem (st.starlist[i]->time);
        ui->tableWidget->setItem(i,0,item_index);

        url[i]=new QPushButton();
        url[i]->setText(st.starlist[i]->text);

        connect(url[i],&QPushButton::clicked,this,[=](){
            this->url_for_open(i);
        });
        delet[i]=new QPushButton();
        delet[i]->setText("删除(X)");
        connect(delet[i],&QPushButton::clicked,this,[=](){
            this->delete_star(i);
        });
        QHBoxLayout *vLayout = new QHBoxLayout();
        QWidget *Widget_btn = new QWidget;


        vLayout->addWidget(url[i]);
        vLayout->addSpacing(10);
        vLayout->addWidget(delet[i]);
        Widget_btn->setLayout(vLayout);
        Widget_btn->setStyleSheet("QWidget{ border: 0px }");
        ui->tableWidget->setCellWidget(i,1,Widget_btn);
        ui->tableWidget->verticalHeader()->setHidden(true);
        i++;
    }
}

void Star_net::url_for_open(int i){
    emit sendData(st.starlist[i]->url);
        this->close();


}

void Star_net::delete_star(int i){
    int rowIndex = ui->tableWidget->currentRow();  //查询选中行
        if (rowIndex > -1)
        {
            ui->tableWidget->removeRow(rowIndex);  //删除选中行
        }
     st.deletenet(st.starlist[i]->url);
}
